var a
var app = getApp()
var t = app.util
var o = app.http.day_salaryPost;
var join_ting = app.http.creat_user_verifyPost        //接口  申请加入厅
var util = getApp().util
Page({
  data: {
    hasLogin: !1,
    userinfo: null,
    gz_week: {},
    hall_info:'', //厅信息
    gz_week_riqi: "",
    gz_param: {
      type: "",
      sdate: "",
      edate: "",
      showLoading: !0,
      page:1
    },
    gz_riqi: t.curRiqi().join('-'),
    gz_data: [],
    gz_arr:[], //存放所有的工资数据
    system:null,
    scroolHeight:0,  //scroll-view的高度
  },
  onLoad: function (a) {

    let that = this
    that.setData({
      gz_goon: 1,  //设置是否调用工资接口
    })
  
  },
  onReady: function () {
    var that = this
    setTimeout(function () {
      // 延迟获取是为了解决真机上 windowHeight 值不对的问题
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            system:res
          });
          //计算scroll 高度
          if(that.hasLogin){
            that.getScrollviewHeight()
          }
         
        }
      });
    }, 500)
    
   
  },
  _onScrollToLower: function () {
    //page 大于 总页面数
    if (this.data.gz_param.page > this.data.gz_param.total_page){
      // a.setData({
      //   finishText: "没有更多"
      // }) 
      //a.loadMoreComplete(!1)
      wx.showToast({
        title: '没有更多数据',
        icon:'none'
      })
     return;
    }else{
      //加载分页
      //a.loadMoreComplete(!0);
      this.gz_everyDay();
      this.setData({
        "gz_param.showLoading":1
      })
    }
  },
  onShow: function () {
    var that = this;

    if(wx.getStorageSync("userinfo")){
      that.setData({
        hasLogin:true,
        userinfo:wx.getStorageSync("userinfo")
      })
    }
     
    // wx.removeStorageSync('hall_info')
    //根据角色判断
    app.getRoles(function (s, msg, res) {
      if(s){ 
          //角色 
          if(res.status == 1){
            var userinfo = wx.getStorageSync("userinfo")
            userinfo.roles = res.roles;
            that.setData({
              hasLogin: !0,
              userinfo,
              "gz_param.page": 1,
              gz_data: []
            })
            //天工资 
            that.gz_everyDay()
            //周工资
            that.gz_week();
         }
      }
    },false);

  },
  onHide: function () {
    this.setData({
      gz_goon:0,
      "gz_param.page": 1,
      gz_data: []
    })
  },
  onUnload: function () {

  },
  onPullDownRefresh: function () { },
  onReachBottom: function () { },
  onShareAppMessage: function () {
    return util.shareSet(this, "工资");
  },
  onLogin: function (a) {
    var that = this;
    app.login(a, function (a, res) {
      if(a){
        wx.showToast({
          title: '登录失败',
          icon:'none'
        })
      }else{
        //设置厅
        var hall_list = res.hall_list;
        if(hall_list && !wx.getStorageSync("share")){
          wx.setStorageSync('hall_info', hall_list[0]);
        }
        that.setData({
          userinfo:wx.getStorageSync("userinfo"),
          hasLogin:true
        })
        that.onShow()
      }
    });
  },
  joinTeam: function () {
    var that = this;
    if(!wx.getStorageSync('hall_info')){
      wx.showToast({
        title: '没有可以申请加入的厅',
        icon: 'none',
        duration: 2000
      })
       return;
    }
    //接口
    join_ting({
      token:wx.getStorageSync('userinfo').token,
      hall_id:wx.getStorageSync('hall_info').id
    }).then(res => {
      if (res.code == 0) {
        let userinfo =  wx.getStorageSync('userinfo')
        userinfo.roles = 0;
        wx.setStorageSync('userinfo', userinfo);
        that.setData({
          userinfo
        })
        wx.showToast({
          title: '请联系管理员认证',
          icon: 'none',
          duration: 2000
        })
      } else {
        wx.showToast({
          title: '接口返回数据失败',
          icon: 'none',
          duration: 2000
        })
      }
    }).catch(err => {
    })
  },
  searchUrl: function (a) {
    wx.navigateTo({
      url: "../../wages/pages/search_detail/search_detail"
    });
  },
  gz_week: function () {
    var a = new Date().getDay();
    this.setData({
      gz_week_riqi: t.curRiqi().join('-')
    })
     0 == a && (this.setData({
      "gz_param.type": "week",
      "gz_param.sdate": "",
      "gz_param.edate": ""
    }), this.gz());
  },
  gz_everyDay: function () {
    var showLoading = 1;
    if (this.data.gz_param.page > 1){
      //showLoading = 0 //第二页不显示加载提示
      //防止同一页面请求两次
      if (this.data.gz_param.page == this.data.gz_param.curpage){        
           return;
      }
    }
    
    this.setData({
      "gz_param.type": "",
      "gz_param.sdate": "2019-12-01",
      "gz_param.edate": this.data.gz_riqi,
    }), this.gz();
  },
  gz: function () {
    var e = this, t = wx.getStorageSync("hall_info").id, n = wx.getStorageSync("userinfo").token, i = this.data.gz_param.type
    var s = this.data.gz_param.sdate
    var r = this.data.gz_param.edate
    var g = this.data.gz_param.showLoading;
    var p = this.data.gz_param.page
 
    if ("week" == i){
       p = 1
    }
    n && o({
      token: n,
      hall_id: t,
      type: i,
      sdate: s,
      edate: r,
      page:p
    }, g).then(function (t) {
      console.log(t,"工资接口")
      if (0 == t.code) {
         //周
        if ("week" == i) {
          e.setData({
            "gz_week.total_salary": t.total_salary,
            "gz_week.datas": t.datas
          });
        } else { //天
          //数据已取完
          if (t.datas.length == 0){

          }else{
            var gz_data = e.data.gz_data
            e.setData({
              gz_data: gz_data.concat(t.datas),
              "gz_param.page": parseInt(p) + 1,
              "gz_param.total_page": t.total_page,
              "gz_param.curpage": p
            })
          }
        }
        //加载提示框
        e.setData({
          "gz_param.showLoading":0
        })
         //计算scrol 高度
          e.getScrollviewHeight();
      }else if(t.code == 1){
          //没有数据
          e.setData({
            "gz_param.showLoading":0  //加载提示框
          })
      } else {
        wx.showToast({
          title: '接口返回数据失败',
          icon:'none'
        })
        console.log("接口返回数据失败", t);
      }
      
    });
  },
 
  //跳转上周工资页面
  gourlweekgz(){
    if (!this.data.userinfo.roles){
       wx.showToast({
         title: '您还没有加入团队~请联系管理员认证加入',
         icon:'none'
       })
       return;
    }
    wx.navigateTo({
      url: "../../wages/pages/week_ranking/week_ranking"
    })
  },
  //获取scroolHeight 的高度
  getScrollviewHeight(){
    let that = this
    let getSystemInfoSync = wx.getSystemInfoSync()
    // 创建节点查询器 query
    let query = wx.createSelectorQuery();
    //获取 class = user
    query.select('.user').boundingClientRect()
    //获取 class = weekimg 
    query.select('.weekimg').boundingClientRect()
    //获取 class =  scrollgao
    query.select('.scrollgao').boundingClientRect()
    //获取 class = tabbar   底部导航
    //query.select('.tabbar').boundingClientRect()

    //计算
    query.exec((res) => {
      let userHeight =  res[0].height                                          //user
      let weekimgHeight = res[1].height                                        //weekimg
      let scrollgaoHeight = res[2].height                                       // scrollgao
     // let tabbarHeight = res[3].height                                       // tabbar
      console.log(getSystemInfoSync)

      let maxscroolHeight = getSystemInfoSync.windowHeight - userHeight - weekimgHeight 
      console.log("可视区高度"+getSystemInfoSync.windowHeight,"用户"+userHeight,"周图片"+weekimgHeight,"计算出scrol最大高度"+maxscroolHeight,"滚动"+scrollgaoHeight);
      let scroolHeight = maxscroolHeight;
      if(scrollgaoHeight < maxscroolHeight){
        scroolHeight = scrollgaoHeight
      }
      that.setData({
        scroolHeight:scroolHeight -1
      })
    })
  },

});